<?php namespace App\Http\Controllers\admin\advertis;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Validator;
use Input;
use App\Models\Ads;
use Carbon\Carbon;
use Intervention\Image\ImageManagerStatic as Image;
use Session;
use Redirect;

class AdvertisController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
        if(Session::has('admin')){
        return view("admin.advertis.index")->withIsAdvertises(true);

        }else{
           return Redirect::to('admin/dologin');
        }
	}

    public function moveAd($image)
    {
    	$dir = base_path() . "/public/images/ad";
    	is_dir($dir) || mkdir($dir, 0777, true);
    	$path = base_path() . "/public/upload/";
    	$temp = $path . $image;
    	$newPath = $dir . "/" . $image;

    	if(is_file($temp)){
    		rename($temp, $newPath);
    	}
    	// Image::make($newPath)->resize(1440, 400)->save($newPath);
    }




    public function addIndexBanner(){

        $data = Input::all();
        $img = Input::get("image");
        $num = Ads::where("type",0)->count();
        if($num < 5){
        	$this->moveAd($img);
        	$data["type"] = 0;
        	Ads::create($data);
        	return $this->json([]);
        }else{
        	return self::error(self::NUMBER_IS_BEYAND);
        }
    }

    // public function addindex(){

    //     $data = Input::all();
    //     $img  = Input::get("image");
    //     $num = Ads::where("title",0)->count();
    // }

     public function addIndustryBanner(){
        $data = Input::all();
        $img = Input::get("image");
        $num = Ads::where("type",1)->count();
        if($num < 5){
        	$this->moveAd($img);
        	$data["type"] = 1;
        	Ads::create($data);
        	return $this->json([]);
        }else{
        	return self::error(self::NUMBER_IS_BEYAND);
        }
    }

    public function addInteractiveBanner(){

        $data = Input::all();
        $img = Input::get("image");
        $num = Ads::where("type",2)->count();
        if($num < 5){
            $this->moveAd($img);
            $data["type"] = 2;
            Ads::create($data);
            return $this->json([]);
        }else{
           return self::error(self::NUMBER_IS_BEYAND);
        }
    }

    public function updateInteractiveBanner(){

        $bid = Input::get("bid");
        $data = Input::except("bid");
        $model = Ads::where("id",$bid);
        if($model->count()){
            if(Input::has("image") && Input::get("image") != ""){
                $this->moveAd(Input::get("image"));
            }
            $model->update($data);
            return $this->json([]);
        }else{
            return;
        }
    }

    public function getInteractiveBanner(){
        $data = Ads::where("type",2)->orderBy("order","desc")->get()->toArray();
        return $this->json(["data"=>$data]);
    }

    public function getIndexBanner(){
    	$data = Ads::where("type", 0)->orderBy("order","desc")->get()->toArray();
    	return $this->json(["data" =>$data]);
    }

    public function getIndustryBanner(){

    	$data = Ads::where("type", 1)->orderBy("order","desc")->get()->toArray();
    	return $this->json(["data" =>$data]);
    }

    public function editInteractiveBanner(){
        $bid = Input::get("bid");
        $model= Ads::where("id",$bid);
        if($model->count()){
            $data = $model->first()->toArray();
            return $this->json(["data" => $data]);
        }

    }

    public function editIndexBanner(){

    	$bid = Input::get("bid");
    	$model= Ads::where("id",$bid);
    	if($model->count()){
    		$data = $model->first()->toArray();
    		return $this->json(["data" => $data]);
    	}
    }

    public function editIndex(){

        $bid = Input::get("bid");
        $model = Ads::where("id",$bid);
        if($model->count()){
            $data = $model->first()->toArray();
            return $this->json(["data" =>$data]);
        }
    }

    public function updateIndexBanner(){
    	$bid = Input::get("bid");
    	$data = Input::except("bid");
    	$model = Ads::where("id",$bid);
    	if($model->count()){
            if(Input::has("image") && Input::get("image") != ""){
                $this->moveAd(Input::get("image"));
            }
    		$model->update($data);
    		return $this->json([]);
    	}else{
    		return;
    	}
    }

    public function updateIndustryBanner(){
        $bid = Input::get("bid");
        $data = Input::except("bid");
        $model = Ads::where("id",$bid);
        if($model->count()){
            if(Input::has("image") && Input::get("image") != ""){
                $this->moveAd(Input::get("image"));
            }
            $model->update($data);
            return $this->json([]);
        }else{
            return;
        }
    }

     public function editIndustryBanner(){

        $bid = Input::get("bid");
        $model= Ads::where("id",$bid);
        if($model->count()){
            $data = $model->first()->toArray();
            return $this->json(["data" => $data]);
        }
    }
          

    // public function updateIndexBanner(){
    //     $bid = Input::get("bid");
    //     $data = Input::except("bid");
    //     $model = Ads::where("id",$bid);
    //     if($model->count()){
    //         $model->update($data);

    //         return $this->json([]);
    //     }else{
    //         return;
    //     }
    // }


   
    public function deleteIndexBanner(){

    	$bid = Input::get("bid");
    	$model = Ads::where("id",$bid);
    	if($model->count()){
    		$model->delete();
    		return $this->json([]);
    	}
    }

    public function deleteIndustryBanner(){

        $bid = Input::get("bid");
        $model = Ads::where("id",$bid);
        if($model->count()){
            $model->delete();
            return $this->json([]);
        }
    }

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
